package io.renren.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.base.Preconditions;
import io.renren.annotation.SysLog;
import io.renren.common.utils.R;
import io.renren.entity.BookmarkEntity;
import io.renren.entity.CampaignEntity;
import io.renren.service.BookmarkService;
import io.renren.service.CampaignService;
import io.renren.util.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-01-20 12:55:41
 */
@RestController
@RequestMapping("api/bookmark")
public class BookmarkController {
    @Autowired
    private BookmarkService bookmarkService;

    @Autowired
    private CampaignService campaignService;



    /**
     * 保存
     */
    @SysLog("添加bookmark")
    @RequestMapping("/save")
    public R save(@RequestBody BookmarkEntity bookmarkEntity){
        try {
            boolean isSuccess = bookmarkService.insertOrUpdate(bookmarkEntity);
            if(isSuccess) {
                return R.ok().put("bookmarkId", bookmarkEntity.getId());
            }else{

                return R.error("操作失败");
            }
        }catch (DuplicateKeyException e){

            return R.error("the campaign already in your bookmark");

        }
        catch (Exception e) {

        }

        return R.error("操作失败");
    }



    /**
     * 删除
     */
    @SysLog("删除bookmark")
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
        bookmarkService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 根据项目查询评论
     */
    @SysLog("查询bookmark")
    @RequestMapping("/infoByUID")
    public R infoByCID(@RequestParam Map<String, Object> params){

        Preconditions.checkArgument(null != params.get("userId"), "用户ID不能为空");

        String userId =params.get("userId").toString();

        Page<BookmarkEntity> bookmarkEntityPage = bookmarkService.selectPage(new Query<BookmarkEntity>(params).getPage(), new EntityWrapper<BookmarkEntity>().eq("user_id", userId));

        List<BookmarkEntity> records = bookmarkEntityPage.getRecords();

        List<CampaignEntity> queryResult = new ArrayList<>();

        for(BookmarkEntity item: records) {
            CampaignEntity campaignEntity = campaignService.selectById(item.getCampaignId());
            campaignEntity.setBookmarkId(item.getId());
            if(null != campaignEntity) {
                queryResult.add(campaignEntity);
            }
        }

        return R.ok().put("bookmarkList", queryResult);
    }




}
